﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace RoketPack
{
    class SelfUpdate
    {
        private static string p_OriginalName = "RoketPack.exe";
        private static string p_TemporaryOldName = "RoketPack.old.exe";

        internal static bool IsRequired()
        {
            DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);
            FileInfo[] files = dir.GetFiles("*.new");

            return (files.Length > 0);
        }

        internal static void Restart(Manager m)
        {
            // Copy ourselves (Windows does not permit renaming files in use).
            string self = Application.ExecutablePath;
            string oldf = Path.Combine(Application.StartupPath, SelfUpdate.p_TemporaryOldName);
            File.Copy(self, oldf);

            // Now create the self-update process which will move the
            // file into place.
            Process proc = new Process();
            proc.StartInfo.FileName = oldf;
            proc.Start();

            // Quit so that the self-update can take place.
            m.SystemIconHide();
            Application.Exit();
        }

        internal static bool IsUpdating()
        {
            return (SelfUpdate.IsRequired() && Application.ExecutablePath.EndsWith(".old.exe"));
        }

        internal static void Update()
        {
            DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);
            FileInfo[] files = dir.GetFiles("*.new");

            foreach (FileInfo f in files)
            {
                string src = f.FullName;
                string dest = null;
                if (f.FullName.EndsWith(".new"))
                    dest = f.FullName.Substring(0, f.FullName.Length - 4);
                else
                    continue;

                // Move the new file in place.
                if (File.Exists(dest))
                {
                    // Ensure the file is deleted before continuing.
                    while (true)
                    {
                        try
                        {
                            File.Delete(dest);
                            break;
                        }
                        catch (IOException ex)
                        {
                            if (!ex.Message.StartsWith("The process cannot access the file"))
                                throw;
                        }
                    }
                }
                File.Move(src, dest);
            }

            // Start the new copy.
            Process proc = new Process();
            proc.StartInfo.FileName = Path.Combine(Application.StartupPath, SelfUpdate.p_OriginalName);
            proc.Start();

            // Quit so that the new version can run.
            Application.Exit();
        }

        internal static void Cleanup()
        {
            // Delete the old RoketPack.old.exe file.
            if (File.Exists(Path.Combine(Application.StartupPath, SelfUpdate.p_TemporaryOldName)))
            {
                // Ensure the file is deleted before continuing.
                while (true)
                {
                    try
                    {
                        File.Delete(Path.Combine(Application.StartupPath, SelfUpdate.p_TemporaryOldName));
                        break;
                    }
                    catch (IOException ex)
                    {
                        if (!ex.Message.StartsWith("The process cannot access the file"))
                            throw;
                    }
                }
            }
        }
    }
}
